<template>
	<!-- 添加烈士亲属 -->
	<view class="building-add">
		<view class="add-content">
			<navigator class="add-item" url="../../Public/People" hover-class="none">
				<view class="item-left">烈士亲属<text>*</text></view>
				<view class="item-right">{{ userId_text }}</view>
			</navigator>
			<view class="add-item">
				<view class="item-left">牺牲者姓名<text>*</text></view>
				<view class="item-right"><input v-model="sacrificeName" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入牺牲者姓名" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">与牺牲者关系<text>*</text></view>
				<view class="item-right"  @click="typeShow=true">{{ relationBtsacrifice_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">牺牲时间<text>*</text></view>
				<view class="item-right" @click="show=true">{{ sacrificeDate }}</view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-1" @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
		<!-- 类型 -->
		<u-select v-model="typeShow" :list="dictList" @confirm="confirmType"></u-select>
		<u-picker mode="time" v-model="show" :params="params" @confirm="confirm"></u-picker>
	</view>
</template>

<script>
	export default{
		data() {
			return{
				userId:'',
				userId_text:'请选择',
				sacrificeName:'', // 牺牲者姓名
				sacrificeDate:'请选择',// 牺牲者时间
				relationBtsacrifice:'',// 与牺牲者关系
				relationBtsacrifice_text:'请选择',
				params: {
					year: true,
					month: true,
					day: true
				},
				show:false,
				typeShow:false,
				dictList:[],
				martyrKinsfolkId:''
			}
		},
		onLoad(options) {
			this.martyrKinsfolkId = options.id
		},
		onShow() {
			let User = uni.getStorageSync('people')
			this.userId_text = User.name
			this.userId = User.id
		},
		mounted() {
			this.getDictionaries()
			this.getMartyrKinsfolkInfoById()
		},
		methods:{
			confirm(e) {
				this.sacrificeDate = e.year + '-' + e.month + '-' + e.day
			},
			confirmType(e) {
				this.relationBtsacrifice = e[0].value
				this.relationBtsacrifice_text = e[0].label
			},
			getDictionaries() {
				let params = {
					dictCode:'relation_bt_family_holder_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.dictList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: '获取医院等级失败'
						})
					})
			},
			getMartyrKinsfolkInfoById() {
				let params = {
					martyrKinsfolkId:this.martyrKinsfolkId
				}
				this.$u.api.getMartyrKinsfolkInfoById(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							this.userId = res.result.userId
							this.userId_text = res.result.userName
							this.sacrificeDate = res.result.sacrificeDate
							this.sacrificeName = res.result.sacrificeName
							this.relationBtsacrifice = res.result.relationBtsacrifice
							this.dictList.forEach(item => {
								if(item.value == res.result.relationBtsacrifice) {
									this.relationBtsacrifice_text = item.label
								}
							})
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 提交
			handleSubmit() {
				if(this.userId == '') {
					return this.$refs.uToast.show({
						title: '请选择烈士亲属'
					})
				}
				if(this.sacrificeName == '') {
					return this.$refs.uToast.show({
						title: '请填写牺牲者姓名'
					})
				}
				if(this.relationBtsacrifice == '') {
					return this.$refs.uToast.show({
						title: '请选择与牺牲者关系'
					})
				}
				if(this.sacrificeDate == '请选择') {
					return this.$refs.uToast.show({
						title: '请选择牺牲时间'
					})
				}
				let params = {
					relationBtsacrifice:this.relationBtsacrifice,
					sacrificeDate:this.sacrificeDate,
					sacrificeName:this.sacrificeName,
					userId:this.userId,
					id:this.martyrKinsfolkId
				}
				uni.removeStorageSync('people')
				console.log(params)
				this.$u.api.editMartyrKinsfolkInfo(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							uni.showToast({
								title:'修改成功',
								icon:'success'
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title:err.message
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			padding-bottom: 110rpx;
			.add-item{
				display: flex;
				justify-content: space-between;
				align-items: flex-start;
				padding: 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					min-width: 100rpx;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					font-size: 27rpx;
					color: #999;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #CCE6FF;
			.btn-1{
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
		}
	}
</style>
